package com.lenovo.smarttraffic.ui.activity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.WorkerThread;
import android.support.v4.view.ViewPager;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.lenovo.smarttraffic.Constant;
import com.lenovo.smarttraffic.InitApp;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.Runable.GetDataRunbale;
import com.lenovo.smarttraffic.Sensor;
import com.lenovo.smarttraffic.bean.EnvironmentBean;
import com.lenovo.smarttraffic.ui.adapter.FragmentAdapter;
import com.lenovo.smarttraffic.ui.fragment.LineChatFragment;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONArray;
import org.json.JSONObject;
import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.FutureTask;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * on 2020-07-21
 * at 11:21
 * @author Caleb.F
 * @Description
 */
public class LineChatActivity extends BaseActivity {
    private ViewPager mMyViewPager;
    private RadioGroup mMyRadioGroup;
    private RadioButton[] mRadioButtons;
    private Sensor sensorType;
    private ScheduledFuture<?> getDataFuture;
    private List<LineChatFragment> mlist = new ArrayList<>();
    private ProgressDialog mProgressDialog;
    private static final String TAG = "LineChatActivity";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mProgressDialog = new ProgressDialog(this);
        //如果extras为空，不是从上一个Activity则数据没开始获取，需要开启线程池
        Bundle extras = getIntent().getExtras();
        ArrayList arrayList = new ArrayList();
        if (extras != null) {
            //从上一个界面跳转而来,显示对应的传感器数据
            sensorType = (Sensor) extras.getSerializable(Constant.LINECHAT_POSITION);

        } else {
            //开启线程池
            getDataFuture = InitApp.getThreadPoolExecutor().scheduleWithFixedDelay(new GetDataRunbale(),
                    0, 3, TimeUnit.SECONDS);
            //从主界面直接跳转，则默认显示第一个
            sensorType = Sensor.Tem;

        }
        initView();
    }




    /**
     *
     */
    private void initView() {
        initToolBar("实时显示");
        initWidgetId();
        ArrayList<EnvironmentBean> beans = new ArrayList<>();
        LineChatFragment temFragment = LineChatFragment.newFragment(beans, Sensor.Tem);
        LineChatFragment humFragment = LineChatFragment.newFragment(beans, Sensor.Tem);
        LineChatFragment lightFragment = LineChatFragment.newFragment(beans, Sensor.Tem);
        LineChatFragment carbonFragment = LineChatFragment.newFragment(beans, Sensor.Tem);
        LineChatFragment pmFragment = LineChatFragment.newFragment(beans, Sensor.Tem);
        LineChatFragment roadFragment = LineChatFragment.newFragment(beans, Sensor.Tem);
        LineChatFragment[] mFragments = new LineChatFragment[]{
                temFragment, humFragment,
                lightFragment, carbonFragment,
                pmFragment, roadFragment
        };
        mlist.addAll(Arrays.asList(mFragments));
        FragmentAdapter fragmentAdapter = new FragmentAdapter(getSupportFragmentManager(), mlist);
        mMyViewPager.setAdapter(fragmentAdapter);
        mMyViewPager.setOffscreenPageLimit(1);
        mMyRadioGroup.setOnCheckedChangeListener((group, checkedId) -> {
            for (int i = 0; i < mRadioButtons.length; i++) {
                if (checkedId == mRadioButtons[i].getId()) {
                    mMyViewPager.setCurrentItem(i);
                }
            }
        });
        mMyViewPager.addOnPageChangeListener(
                new ViewPager.SimpleOnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) {
                        for (RadioButton radioButton : mRadioButtons) {
                            radioButton.setChecked(false);
                        }
                        mRadioButtons[position].setChecked(true);
                    }
                }
        );

        mMyViewPager.setCurrentItem(sensorType.getPos());
        mRadioButtons[sensorType.getPos()].setChecked(true);

    }

    /**
     * 初始化控件id
     */
    private void initWidgetId() {
        /*
         * 关于radioButton的设置
         * https://blog.csdn.net/qq_23992393/article/details/84934550?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.nonecase
         * */
        mMyViewPager = findViewById(R.id.my_view_pager);
        mMyRadioGroup = findViewById(R.id.my_radio_group);
        RadioButton mRadioTem = findViewById(R.id.radio_tem);
        RadioButton mRadioHum = findViewById(R.id.radio_hum);
        RadioButton mRadioLight = findViewById(R.id.radio_light);
        RadioButton mRadioCarbon = findViewById(R.id.radio_carbon);
        RadioButton mRadioPm = findViewById(R.id.radio_pm);
        RadioButton mRadioRoad = findViewById(R.id.radio_road);
        mRadioButtons = new RadioButton[]{
                mRadioTem, mRadioHum, mRadioLight, mRadioCarbon, mRadioPm, mRadioRoad
        };

    }


    @Override
    protected int getLayout() {
        return R.layout.activity_linechat;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (getDataFuture != null) {
            getDataFuture.cancel(true);
        }
    }
}
